package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.mgrsite.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    @Value("${file.path}")
    private String basePath;

    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object uploadImage(MultipartFile pic) {
        if (pic == null || pic.isEmpty()) {
            return null;
        }
        return UploadUtil.upload(pic, basePath);
    }

    @RequestMapping("/uploadImg_ck")
    @ResponseBody
    public Map<String, Object> uploadImgCk(MultipartFile upload) {
        Map<String, Object> map = new HashMap<>();
        try {
            String path = UploadUtil.upload(upload, basePath);
            map.put("uploaded", 1);
            map.put("url", "/" + path);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded", 0);
            Map<String, Object> map1 = new HashMap<>();
            map1.put("message", "图片上传错误!!!!!");
            map.put("error", map1);
            return map;

        }
    }

}
